<?php
namespace App\Socket\Logic;
use EasySwoole\Core\Socket\Client\WebSocket;
use App\Socket\Model\Room;
class GameLogic extends Base{

    /**
     * 用户准备     
     * @param [type] $fd
     * @return void
     */
    public static function ready(WebSocket $client){        
        $user = $client->getData();    
        $rid = $client->getFd();
        $room = $user->Room;

        $ready = $room->ready;        
        $ready[$user->uid] = true;                 

        $room->ready = $ready;               
        $room->save();
        
        $res = [
            "cmd"=>"showReadyText",
            "data"=>[
                "uid"=>$user->uid
            ]
        ];
        self::notice($res,$room);

        if(count($ready)==3){
            self::Licensing($room);
        }    
    }


    /** 准备完后开始发牌 */
    public static function Licensing(Room $room){
        
        $deckCards = new DeckCards();
        $deckCards->create();                

        $point = $room->point;

      
        $handCard = [];
        foreach($point as $uid){
            $handCard[$uid] = $deckCards->getCards(17);            
        }
        $room->baseCard = $deckCards->getBottom();
        $room->handCard = $handCard;
        $room->save();

        $users = $room->Users;
        foreach($users as $user){
            $uid = $user->uid;
            $res = [
                "cmd"=>"Licensing",
                "data"=>[
                    [
                        "uid"=>$uid,
                        "cards"=>$handCard[$uid],
                        "bottom"=>$room->baseCard
                    ]
                ]
            ];

            foreach($users as $user2){
                $uid2 = $user2->uid;
                if($uid!=$uid2){
                    $res["data"][] = [
                        "uid"=>$uid2,
                        "cards"=>count($handCard[$uid2])
                    ];
                }
            }
            self::push($user->fd,$res);
        }

        \Swoole\Timer::after(1*1000,function()use($room){            
            self::showFractionPanel($room);
        });   
    }
 
    
    /**
     * 显示抢地主面板     
     * @return void
     */
    public static function showFractionPanel(Room $room){        
        //发完牌后，显示叫地主功能。                
        $users = $room->Users;
        $user = $users[0];
        $msg = [
            "cmd"=>"showFractionPanel",
            "data"=>[
                "uid"=> $user->uid,
                "max"=>0
            ]
        ];            
        self::notice($msg,$room);            
    }


        /**
     * 抢地主     
     * @param [type] $fd
     * @param [type] $number
     * @return void
     */
    public static function faction(WebSocket $client,$number){
        $user = $client->getData();
        $fd = $client->getFd();
        $room = $user->Room;

        $score = $room->score;
        if($score &&  array_key_exists($user->uid,$score) ){
            return ;
        }

       
        $score[$user->uid] = $number;
        $room->score = $score;
        $room->save();

        if($number==3){            
            $room->landlord = $user->uid;
            $room->nextOutUid = $user->uid;
            $room->save();
            //设置好地主
            //抢地主完成。将底牌发给地主
            self::LicensingBottom($room);
            return;
        }


        if(count($score)==3){
            //叫分结算 设置阵营
            $max = max($score);
            $landlord = array_search($max,$score);
            $room->landlord = $landlord;
            $room->nextOutUid = $landlord;
            $room->save();
            //设置好地主
            //抢地主完成。将底牌发给地主
            self::LicensingBottom($room);
            return;
        }else{
            //通知下一个人抢分
            $point = $room->point;
            $index = array_search($user->uid,$point);                                    
            $next = $index+1;                                    
            $nextUid = $point[$next];            
            
            $msg = [
                "cmd"=>"showFractionPanel",
                "data"=>[
                    "uid"=> $nextUid,
                    "max"=>$number
                ]
            ];
            self::notice($msg,$room);
        }        
    }


    /**
     * 发底牌
     */
    public static function LicensingBottom(Room $room){        
        //将底牌加入到地主的手牌中
        $base = $room->baseCard;
        $land = $room->landlord;
        $handCard = $room->handCard;
        $landCard = $handCard[$land];
        $landCard = array_merge($landCard,$base);
        $handCard[$land] = $landCard;
        $room->handCard = $handCard;
        $room->save();

        $msg = [
            "cmd"=>"LicensingBottom",
            "data"=>[
                "uid"=>$room->landlord
            ]
        ];        
        self::notice($msg,$room);
    }


    /**
     * 出牌     
     * @param WebSocket $client
     * @param [type] $param  
     *    $param = [
     *       [
     *           "name"=>"King",
     *           "color"=>"Heart"
     *       ],
     *       [
     *           "name"=>"King",
     *           "color"=>"Club"
     *       ]
     *    ];  
     * @return void
     */
    public static function outCard(WebSocket $client,$param){
        $fd = $client->getFd();
        $user = $client->getData();
        $room = $user->Room;
        //p($user->toArray());
        //首先判断是否是当前用户出牌
        if($user->uid!=$room->nextOutUid){
            //不是当前用户出牌
            return ;
        }

        //判断当前用户的牌型
        $current = null;
        if($room->outCard && $room->outCard["uid"]!=$user->uid){
            $current = $room->outCard["info"];           
        }

      
        $check =  new CheckOutCards($param,$current);
        $info = $check->check();
          
        p($info);
        if($info){
            
            $room->outCard = [
                "info"=>$info,
                "cards"=>$param,
                "uid"=>$user->uid
            ];                             
            $handCard =  $room->handCard;          
            $uHand = $handCard[$user->uid];         
          
            foreach($param as $item){            
                $index = array_search($item,$uHand);
                if($index!==false){                    
                    array_splice($uHand,$index,1);                                   
                }                    
            }  


        
            

            $handCard[$user->uid] = $uHand;
            $room->handCard = $handCard;
            
            //指定下一个出牌的人
            $point = $room->point;
        
            $index =array_search($user->uid,$point);
            if($index>=count($point)-1){
                $next = $point[0];
            }else{
                $next = $point[$index+1];
            }
        
            $room->nextOutUid = $next;
            $room->save();
     
            $msg=[
                "cmd"=>"receiveOutCards",
                "data"=>[
                    "uid"=>$user->uid,
                    "cards"=>$param,
                    "next"=>$next
                ]
            ];   
            
            if($room->outCard && $next==$room->outCard["uid"]){
                $msg["data"]["new"] =1;
            }
            self::notice($msg,$room);

            //如果手牌为空，则游戏结束
            if( count($uHand)==0 ) {
                $msg = [
                    "cmd"=>"showGameOverPanel",
                    "data"=>[
                        "winUid"=>$user->uid,
                    ]                    
                ];
                self::notice($msg,$room);
                return ;
            }


        }else{
            return false;
        }
    }

    /**
     * 跳过出牌     
     * @param WebSocket $client
     * @return void
     */
    public static function jump(WebSocket $client){
        $fd = $client->getFd();
        $user = $client->getData();
        $room = $user->Room;
        
        
        //指定下一个出牌的人
        $point = $room->point;
        
        $index =array_search($user->uid,$point);
        
        if($index>=count($point)-1){
            $next = $point[0];
        }else{
            $next = $point[$index+1];
        }
        
        $room->nextOutUid = $next;
        $room->save();
        $msg=[
            "cmd"=>"receiveOutCards",
            "data"=>[
                "uid"=>$user->uid,                
                "next"=>$room->nextOutUid
            ]
        ];   
        if($room->outCard && $next==$room->outCard["uid"]){
            $msg["data"]["new"] =1;
        }                 
        self::notice($msg,$room);        
    }
}